import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import { tbdetailsofachievements } from '../../../atoms/db';

const logger = anylogger('pages/api/courses/coursetaken.api');

export type IData = {
	curriculum_id: any;
	credit: any;
	coursename: any;
	student_id: any;
	totalscore: any;
	type: any;
}[];

export type Result = {
	ok: true;

	//必修
	data: IData;
	//创新实践
	data1: IData;
	//选修
	data2: IData;
} | {
	ok: false;
	message: string;


}


/**
 * pages/api/courses/coursetaken.api
 */
const handler = an48<Result>();

handler.post(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);
		const data = await tbdetailsofachievements()
			.select({
				coursename: ' detailsofachievements.coursename',
				curriculum: 'curriculum.curriculum_id',
				totalscore: 'detailsofachievements.totalscore',
				type: 'curriculum.type',
				curriculum_id: 'detailsofachievements.curriculum_id',
				credit: 'curriculum.dredit',
				student_id: 'detailsofachievements.student_id'
			})
			.leftJoin('curriculum', 'detailsofachievements.curriculum_id', 'curriculum.curriculum_id'
			).where({ 'curriculum.type': '必修' }).andWhere({ 'student_id': '201942307789' });


		const data1 = await tbdetailsofachievements().select({
			coursename: ' detailsofachievements.coursename',
			curriculum: 'curriculum.curriculum_id',
			totalscore: 'detailsofachievements.totalscore',
			type: 'curriculum.type',
			curriculum_id: 'detailsofachievements.curriculum_id',
			credit: 'curriculum.dredit',
			student_id: 'detailsofachievements.student_id'
		})



			.leftJoin('curriculum', 'detailsofachievements.curriculum_id', 'curriculum.curriculum_id'
			).where({ 'curriculum.type': '创新实践' }).andWhere({ 'student_id': '201942307789' });

		const data2 = await tbdetailsofachievements().select({
			coursename: ' detailsofachievements.coursename',
			curriculum: 'curriculum.curriculum_id',
			totalscore: 'detailsofachievements.totalscore',
			type: 'curriculum.type',
			curriculum_id: 'detailsofachievements.curriculum_id',
			credit: 'curriculum.dredit',
			student_id: 'detailsofachievements.student_id'
		})
			.leftJoin('curriculum', 'detailsofachievements.curriculum_id', 'curriculum.curriculum_id'
			).where({ 'curriculum.type': '选修' }).andWhere({ 'student_id': '201942307789' });


		res.status(200).json({ data, data1, data2, ok: true, });
	} catch (error) {
		logger.trace(error);
		res.status(200).json({ ok: false, message: (error as Error).message });
	}
});

export const config = {} as PageConfig;

export default handler;
